package com.sikaryofficial.system.manager;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Maps;
import com.sikaryofficial.common.core.exception.ServiceException;
import com.sikaryofficial.system.api.domain.SysUser;
import com.sikaryofficial.system.domain.ChannelGoodsScopeReq;
import com.sikaryofficial.system.domain.Customer;
import com.sikaryofficial.system.domain.dto.SysChannelUserListDTO;
import com.sikaryofficial.system.service.ICustomerService;
import com.sikaryofficial.system.service.IGoodsScopeService;
import com.sikaryofficial.system.service.ISysUserService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author : qinjinyuan
 * @desc : 渠道商用户管理 聚合类
 * @date : 2024/03/02 21:08
 */
@Service
public class UserChannelManager {
    @Autowired
    private IGoodsScopeService goodsScopeService;
    @Autowired
    private ICustomerService customerService;

    @Autowired
    private ISysUserService sysUserService;

    @DS("hismk_workbench")
    public void buildOtherInfo(List<SysChannelUserListDTO> list) {
        if (CollUtil.isNotEmpty(list)) {
            List<Long> userIds = list.stream().map(SysChannelUserListDTO::getUserId).collect(Collectors.toList());
            List<Customer> customers = customerService.list(new LambdaQueryWrapper<Customer>()
                    .eq(Customer::getDeletedVersion, 0L)
                    .in(Customer::getUserId, userIds)
                    .isNotNull(Customer::getLanguage)
            );
            Map<Long, String> customerMap = Maps.newHashMap();
            if (CollUtil.isNotEmpty(customers)) {
                customerMap = customers.stream().filter(e -> Objects.nonNull(e.getUserId()) && StringUtils.isNotBlank(e.getLanguage()))
                        .collect(Collectors.toMap(Customer::getUserId, Customer::getLanguage, (k1, k2) -> k1));
            }
            for (SysChannelUserListDTO userListDTO : list) {
                userListDTO.setLanguage(MapUtils.getObject(customerMap, userListDTO.getUserId(), "-"));
            }
        }
    }

    /**
     * @param req
     */
    public Boolean allotGoodsScope(@Validated @RequestBody ChannelGoodsScopeReq req) {
        if(CollUtil.isEmpty(req.getGoodsIds())){
            throw new ServiceException("请选择要分配的商品");
        }
        List<SysUser> userList =  sysUserService.selectUserByIds(req.getUserIds());
        if(CollUtil.isEmpty(userList)){
            throw new ServiceException("选择用户异常");
        }
        for (SysUser sysUser : userList){
            if(StringUtils.equals("1", sysUser.getStatus())){
                throw new ServiceException("选择用户已注销，firstName_lastName: "+ sysUser.getFirstName() + "  " + sysUser.getLastName() );
            }
        }
        return goodsScopeService.allotGoodsScope(req);
    }
}
